local skel = fk.CreateSkill {
  name = "emo__dayin",
  dynamic_desc = function (self, player, lang)
    if player:usedSkillTimes("emo__qidian", Player.HistoryGame) > 0 then
      return "emo__dayin_adv"
    end
    return "emo__dayin_beg"
  end,
}

local U = require "packages.utility.utility"

skel:addEffect("active", {
  anim_type = "drawcard",
  card_num = 0,
  target_num = 0,
  target_filter = Util.FalseFunc,
  prompt = "#emo__dayin",
  can_use = function (self, player)
    return player:getMark("@emo__sucaidian") > 1
  end,
  interaction = function(self, player)
    local all_choices = { "emo__dayin_basic", "emo__dayin_trick", "emo__dayin_equip" }
    local choices = table.simpleClone(all_choices)
    if player:usedSkillTimes("emo__qidian", Player.HistoryGame) == 0 or player:getMark("@emo__sucaidian") < 5 then
      table.remove(choices, 3)
    end
    if player:getMark("@emo__sucaidian") < 4 then
      table.remove(choices, 2)
    end
    return UI.ComboBox { choices = choices, all_choices = all_choices }
  end,
  on_use = function (self, room, use)
    local player = use.from
    local choice = self.interaction.data
    if choice == nil then return end
    local cardType = choice:sub(-5, -1)
    local adv = player:usedSkillTimes("emo__qidian", Player.HistoryGame) > 0
    local num = cardType == "basic" and 2 or (cardType == "trick" and 4 or 5)
    room:removePlayerMark(player, "@emo__sucaidian", num)
    local t = cardType == "basic" and "b" or (cardType == "trick" and "td" or "e")
    local names = Fk:getAllCardNames(t)
    local name = U.askForChooseCardNames(room, player, names, 1, 1, skel.name, "#emo__dayin-name",
    names, false, false)[1]
    local copy = Fk.all_card_types[name]
    if name then
      local card = room:printCard(name, math.random(4), math.random(13))
      room:setCardMark(card, "@@emo__dayin", 1)
      room:obtainCard(player, card, false, fk.ReasonJustMove, player, skel.name)
    end
    if adv and not player.dead then
      local ids = room:getCardsFromPileByRule(".|.|.|.|.|"..cardType, 1, "allPiles")
      if #ids > 0 then
        room:obtainCard(player, ids, true, fk.ReasonJustMove, player, skel.name)
      end
    end
  end,
})

skel:addEffect(fk.TurnStart, {
  anim_type = "drawcard",
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill(skel.name) and not player:isNude()
  end,
  on_cost = function (self, event, target, player, data)
    local cards = player.room:askToCards(player, {
      min_num = 1, max_num = 1, skill_name = skel.name, include_equip = true,
      prompt = "#emo__dayin-put",
    })
    if #cards == 0 then return false end
    event:setCostData(self, { cards = cards })
    return true
  end,
  on_use = function (self, event, target, player, data)
    local cards = event:getCostData(self).cards
    player:addToPile(skel.name, cards, true, skel.name)
    if player.dead then return end
    local card = Fk:getCardById(cards[1])
    local cardType = card.type
    local num = cardType == Card.TypeBasic and 1 or (cardType == Card.TypeTrick and 2 or 3)
    player.room:addPlayerMark(player, "@emo__sucaidian", num)
  end,
})


Fk:loadTranslationTable{
  ["emo__dayin"] = "打印",
  [":emo__dayin"] = "每名角色回合开始时，你可以将一张牌置于自己的武将牌上，根据牌的类别（基本牌/锦囊牌/装备牌）获得1点/2点/3点素材点。"..
  "<br>出牌阶段，你可以消耗2/4点素材获得一张任意基本/锦囊牌的复制。"..
  "<br>修改：可以消耗5点打印装备牌。你打印牌时，获得一张同类型牌。",

  [":emo__dayin_beg"] = "每名角色回合开始时，你可以将一张牌置于自己的武将牌上，根据牌的类别（基本牌/锦囊牌/装备牌）获得1点/2点/3点素材点。"..
  "<br>出牌阶段，你可以消耗2/4点素材获得一张任意基本/锦囊牌的复制。",
  [":emo__dayin_adv"] = "每名角色回合开始时，你可以将一张牌置于自己的武将牌上，根据牌的类别（基本牌/锦囊牌/装备牌）获得1点/2点/3点素材点。"..
  "<br>出牌阶段，你可以消耗2/4/5点素材获得一张任意基本/锦囊/装备牌的复制，并获得一张同类型牌。",

  ["@emo__sucaidian"] = "素材点",
  ["emo__dayin_basic"] = "2点:基本牌",
  ["emo__dayin_trick"] = "4点:锦囊牌",
  ["emo__dayin_equip"] = "5点:装备牌",

  ["#emo__dayin-name"] = "打印：选择你要打印牌的牌名！",
  ["#emo__dayin"] = "打印：消耗素材点，打印并获得指定类型的牌",
  ["#emo__dayin-put"] = "你可以将一张牌置于自己的武将牌上，基本牌/锦囊牌/装备牌获得1点/2点/3点素材点",
  ["@@emo__dayin"] = "打印",
}

return skel
